﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using HomeBuyerTools.ViewModels;

namespace HomeBuyerTools.Views.RentVSBuy
{
    public partial class RentVSBuy : PhoneApplicationPage
    {
        Custom objCustom = new Custom();
        public RentVSBuy()
        {
            InitializeComponent();
            LoadPivotThemes();
            Loaded += RentVSBuy_Loaded;
        }

        void RentVSBuy_Loaded(object sender, RoutedEventArgs e)
        {
            Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

            if (darkBackgroundVisibility == Visibility.Visible)
            {
                //Theme is Dark
            }

            else
            {
                //Theme is Light
                txtMonthlyRent.MouseEnter += txtMonthlyRent_MouseEnter;
                txtMonthlyRent.MouseLeave += txtMonthlyRent_MouseLeave;
                txtMRInsurance.MouseEnter += txtMRInsurance_MouseEnter;
                txtMRInsurance.MouseLeave += txtMRInsurance_MouseLeave;
                txtEAIR.MouseEnter += txtEAIR_MouseEnter;
                txtEAIR.MouseLeave += txtEAIR_MouseLeave;
                txtPPofHome.MouseEnter += txtPPofHome_MouseEnter;
                txtPPofHome.MouseLeave += txtPPofHome_MouseLeave;
                txtDownPA.MouseEnter += txtDownPA_MouseEnter;
                txtDownPA.MouseLeave += txtDownPA_MouseLeave;
                txtLofMT.MouseEnter += txtLofMT_MouseEnter;
                txtLofMT.MouseLeave += txtLofMT_MouseLeave;
                txtAIRofM.MouseEnter += txtAIRofM_MouseEnter;
                txtAIRofM.MouseLeave += txtAIRofM_MouseLeave;
                txtDiscountPoints.MouseEnter += txtDiscountPoints_MouseEnter;
                txtDiscountPoints.MouseLeave += txtDiscountPoints_MouseLeave;
                txtOriginationFees.MouseEnter += txtOriginationFees_MouseEnter;
                txtOriginationFees.MouseLeave += txtOriginationFees_MouseLeave;
                txtOtherLCosts.MouseEnter += txtOtherLCosts_MouseEnter;
                txtOtherLCosts.MouseLeave += txtOtherLCosts_MouseLeave;
                txtMIPMI.MouseEnter += txtMIPMI_MouseEnter;
                txtMIPMI.MouseLeave += txtMIPMI_MouseLeave;
                txtHIR.MouseEnter += txtHIR_MouseEnter;
                txtHIR.MouseLeave += txtHIR_MouseLeave;
                txtMonthlyAD.MouseEnter += txtMonthlyAD_MouseEnter;
                txtMonthlyAD.MouseLeave += txtMonthlyAD_MouseLeave;
                txtAverageMM.MouseEnter += txtAverageMM_MouseEnter;
                txtAverageMM.MouseLeave += txtAverageMM_MouseLeave;
                txtAnnualPT.MouseEnter += txtAnnualPT_MouseEnter;
                txtAnnualPT.MouseLeave += txtAnnualPT_MouseLeave;
                txtSPFITRate.MouseEnter += txtSPFITRate_MouseEnter;
                txtSPFITRate.MouseLeave += txtSPFITRate_MouseLeave;
                txtIRonSavings.MouseEnter += txtIRonSavings_MouseEnter;
                txtIRonSavings.MouseLeave += txtIRonSavings_MouseLeave;
                txtHomePR.MouseEnter += txtHomePR_MouseEnter;
                txtHomePR.MouseLeave += txtHomePR_MouseLeave;
                txtNofYEatP.MouseEnter += txtNofYEatP_MouseEnter;
                txtNofYEatP.MouseLeave += txtNofYEatP_MouseLeave;
                txtRelatorCR.MouseEnter += txtRelatorCR_MouseEnter;
                txtRelatorCR.MouseLeave += txtRelatorCR_MouseLeave;

            }
            Calculate();
        }

       
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
          
            Calculate();
           NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=4&", UriKind.Relative));
        }
        private void LoadPivotThemes()
        {
            LayoutRoot.Background = Custom.getPivotBackground();
            
        }
        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=4&", UriKind.Relative));

        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        void txtRelatorCR_MouseLeave(object sender, MouseEventArgs e)
        {
            txtRelatorCR.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtRelatorCR_MouseEnter(object sender, MouseEventArgs e)
        {
            txtRelatorCR.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtNofYEatP_MouseLeave(object sender, MouseEventArgs e)
        {
            txtNofYEatP.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtNofYEatP_MouseEnter(object sender, MouseEventArgs e)
        {
            txtNofYEatP.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtHomePR_MouseLeave(object sender, MouseEventArgs e)
        {
            txtHomePR.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtHomePR_MouseEnter(object sender, MouseEventArgs e)
        {
            txtHomePR.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtIRonSavings_MouseLeave(object sender, MouseEventArgs e)
        {
            txtIRonSavings.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtIRonSavings_MouseEnter(object sender, MouseEventArgs e)
        {
            txtIRonSavings.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtSPFITRate_MouseLeave(object sender, MouseEventArgs e)
        {
            txtSPFITRate.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtSPFITRate_MouseEnter(object sender, MouseEventArgs e)
        {
            txtSPFITRate.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtAnnualPT_MouseLeave(object sender, MouseEventArgs e)
        {
            txtAnnualPT.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtAnnualPT_MouseEnter(object sender, MouseEventArgs e)
        {
            txtAnnualPT.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtAverageMM_MouseLeave(object sender, MouseEventArgs e)
        {
            txtAverageMM.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtAverageMM_MouseEnter(object sender, MouseEventArgs e)
        {
            txtAverageMM.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtMonthlyAD_MouseLeave(object sender, MouseEventArgs e)
        {
            txtMonthlyAD.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtMonthlyAD_MouseEnter(object sender, MouseEventArgs e)
        {
            txtMonthlyAD.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtHIR_MouseLeave(object sender, MouseEventArgs e)
        {
            txtHIR.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtHIR_MouseEnter(object sender, MouseEventArgs e)
        {
            txtHIR.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtMIPMI_MouseLeave(object sender, MouseEventArgs e)
        {
            txtMIPMI.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtMIPMI_MouseEnter(object sender, MouseEventArgs e)
        {
            txtMIPMI.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtOtherLCosts_MouseLeave(object sender, MouseEventArgs e)
        {
            txtOtherLCosts.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtOtherLCosts_MouseEnter(object sender, MouseEventArgs e)
        {
            txtOtherLCosts.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtOriginationFees_MouseLeave(object sender, MouseEventArgs e)
        {
            txtOriginationFees.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtOriginationFees_MouseEnter(object sender, MouseEventArgs e)
        {
            txtOriginationFees.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtDiscountPoints_MouseLeave(object sender, MouseEventArgs e)
        {
            txtDiscountPoints.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtDiscountPoints_MouseEnter(object sender, MouseEventArgs e)
        {
            txtDiscountPoints.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtAIRofM_MouseLeave(object sender, MouseEventArgs e)
        {
            txtAIRofM.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtAIRofM_MouseEnter(object sender, MouseEventArgs e)
        {
            txtAIRofM.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtLofMT_MouseLeave(object sender, MouseEventArgs e)
        {
            txtLofMT.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtLofMT_MouseEnter(object sender, MouseEventArgs e)
        {
            txtLofMT.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtDownPA_MouseLeave(object sender, MouseEventArgs e)
        {
            txtDownPA.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtDownPA_MouseEnter(object sender, MouseEventArgs e)
        {
            txtDownPA.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtPPofHome_MouseLeave(object sender, MouseEventArgs e)
        {
            txtPPofHome.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtPPofHome_MouseEnter(object sender, MouseEventArgs e)
        {
            txtPPofHome.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtEAIR_MouseLeave(object sender, MouseEventArgs e)
        {
            txtEAIR.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtEAIR_MouseEnter(object sender, MouseEventArgs e)
        {
            txtEAIR.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtMRInsurance_MouseLeave(object sender, MouseEventArgs e)
        {
            txtMRInsurance.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtMRInsurance_MouseEnter(object sender, MouseEventArgs e)
        {
            txtMRInsurance.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtMonthlyRent_MouseLeave(object sender, MouseEventArgs e)
        {
            txtMonthlyRent.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtMonthlyRent_MouseEnter(object sender, MouseEventArgs e)
        {
            txtMonthlyRent.Foreground = new SolidColorBrush(Colors.White);
        }

        //private void Calculate_click(object sender, RoutedEventArgs e)
        //{
        public void Calculate()
        {
            if (txtMonthlyRent.Text =="" || txtMonthlyRent.Text == Convert.ToString(0))
            {
                MessageBox.Show("Please enter the amount of your monthly rent payment.");
                txtMonthlyRent.Focus();
            }
            else
                if (txtPPofHome.Text == "" || txtPPofHome.Text == Convert.ToString(0))
                {
                    MessageBox.Show("please enter the purchase of the home.");
                    txtPPofHome.Focus();
                }
                else
                    if (txtLofMT.Text == "" || txtLofMT.Text == Convert.ToString(0))
                    {
                        MessageBox.Show("please your number of years you are finacing the home for.");
                        txtLofMT.Focus();
                    }
                    else
                        if (txtNofYEatP.Text == "" || txtNofYEatP.Text == Convert.ToString(0))
                        {
                            MessageBox.Show("please enter the number of years you plan to stay in this property.");
                            txtNofYEatP.Focus();
                        }
                        else
                        {
                            //Get RENTAL INFO
                       var  vmoRent =Convert.ToDouble(txtMonthlyRent.Text);
                            var vtotRent = 0.0;
                      var  vmoRentIns = Convert.ToDouble( txtMRInsurance.Text);
                            if (Convert.ToString(vmoRentIns) == ""  || vmoRentIns ==0)
                            {
                                vmoRentIns =0;
                            }
                         var vinflateRate = Convert.ToDouble(txtEAIR.Text);
                            if( Convert.ToString(vinflateRate) =="" || vinflateRate == 0)
                            {
                                vinflateRate = 0;

                            }
                            else
                                if (vinflateRate >=1)
                                {
                                    vinflateRate = vinflateRate / 100;
                                }
                            vinflateRate = vinflateRate + 1;

                            //GET TIME INFO & ConVERT TO MoNTHS
                           var vstayYrs = Convert.ToDouble(txtNofYEatP.Text);
                           var vstayMonths = vstayYrs * 12;
                            var count = 0;

                            //GET LOAN INFO
                        var i =Convert.ToDouble(txtAIRofM.Text);
                            if (i > 1.0)
                            {
                                i = i / 100.0;

                            }
                            i /= 12;
                           var vhomeCost = Convert.ToDouble(txtPPofHome.Text);
                            var vnoYears = Convert.ToDouble(txtLofMT.Text);
                           var vdownPmt = Convert.ToDouble(txtDownPA.Text);
                           var vorigPrin = vhomeCost - vdownPmt;
                           double intPort = 0.0;
                          var vaccumInt = 0.0;
                          var prinPort=0.0;
                         var prin =vorigPrin;

                            //CALULCATE MONTHLY MORTGAGE PAYMENT    
                           var noMonths = vnoYears * 12;
                        var Pow = 1.0;
                       for (var j = 0; j < noMonths; j++)
                        Pow = Pow * (1 + i);
                            double vmoPmt =(vorigPrin * Pow * i) / (Pow - 1);

                            //GET HOME APPRECIATION INFO
                           var vapprecRate = Convert.ToDouble(txtHomePR.Text);
                            if (Convert.ToString( vapprecRate) =="" || vapprecRate == 0)
                            {
                                vapprecRate = 0;
                            }
                            else
                                if (vapprecRate >= 0)
                                {
                                    vapprecRate = vapprecRate / 100;
                                }
                            vapprecRate = vapprecRate + 1;
                           var vaccumApprec = vhomeCost * vapprecRate;
                            //GET PMI(PRIVATE MORTGAGE INSURANCE) INFO
                           var vpmi = Convert.ToDouble(txtMIPMI.Text);
                            if (vpmi == 0 ||Convert.ToString(vpmi)=="")
                            {
                                vpmi = 0;
                            
                            }
                            else
                                if (vpmi >= .01)
                                {
                                    vpmi = vpmi / 100.0;

                                }
                             vpmi /=12;
                           var pmiYN = 0;
                           var vaccumPmi = 0.0;
                            var downPayPerc = vdownPmt / vhomeCost;
                            if (downPayPerc < .20)
                            {
                                pmiYN = 1;
                                vaccumPmi = 0.0;
                            }

                            //***************CALCULATE CLOSING COSTS
                            //POINTS
                          var vfees = Convert.ToDouble(txtOriginationFees.Text);
                            if (vfees == 0 ||Convert.ToString(vfees) =="")
                            {
                                vfees = 0;
                            }
                            else
                                if (vfees >= 1)
                                {
                                   vfees = vfees / 100;
                                }
                          var vfeesCost = vorigPrin * vfees;

                            //ORIGINATION FEE
                           var vpoints = Convert.ToDouble(txtDiscountPoints.Text);
                            if (vpoints == 0 ||Convert.ToString(vpoints) =="")
                            {
                                vpoints = 0;
                            }
                            else
                                if (vpoints >= 1)
                                {
                                   vpoints=vpoints/100;
                                }
                           var vpointCost = vorigPrin * vpoints;
                            //OTHER LOAN COSTS
                           var vloanCosts = Convert.ToDouble(txtOtherLCosts.Text);
                            if (vloanCosts == 0 ||Convert.ToString(vloanCosts) =="")
                            {
                                vloanCosts = 0;
                            }

                            //TOTAL CLOSING COSTS
                          var vclosingCosts = vpointCost + vfeesCost + vloanCosts;

                            //GET INVESTMENT INFO

                         var vinvestInPort = 0.0;
                          var vinvestPrin = Convert.ToDouble(vdownPmt + vclosingCosts);
                            double earnInt =Convert.ToDouble(txtIRonSavings.Text);
                            if (earnInt >= 1.0)
                            {
                                earnInt = earnInt / 100.0;
                            }
                            earnInt /= 12;

                            //INITIATE INFLATION FACTOR

                          var vaccumInflate = 1.0;

                            //**************CYCLE THORUGH NUMBER OF MONTHS

                            while (count < vstayMonths)
                            {
                                //ACCUMULATE RENT PAYMENTS & INSURANCE & APPRECIATION
                                if (count > 0 && count % 12 == 0)
                                {
                                    vaccumApprec = vaccumApprec * vapprecRate;
                                    vmoRent = vmoRent * vinflateRate;
                                    vaccumInflate = vaccumInflate * vinflateRate;
                                }

                                vtotRent = vtotRent + vmoRent;
                                vtotRent = vtotRent + vmoRentIns;

                                //ACCUMULATE INTERSET PAYMENTS

                                if (count < noMonths)
                                {
                                    intPort = prin * i;
                                    vaccumInt = vaccumInt + intPort;
                                    prinPort =vmoPmt - intPort;
                                    prin =prin-prinPort;
                                }

                                //IF PMI APPLICABLE , ACCUMULATE
                                if (pmiYN == 1)
                                {
                                    vaccumPmi = vaccumPmi + (vpmi * prin);
                                }

                                //AMORTIZE INVESTED DOWNPAYMENT AND CLOSING COSTS

                                vinvestInPort =earnInt * vinvestPrin;
                                vinvestPrin = vinvestPrin + vinvestInPort;

                                //INCREASE COUNT
                                count = count + 1;
                            }

                            //CALCULATE TOTAL ASSOCIATION DUES
                           var vassocDues =Convert.ToDouble(txtMonthlyAD.Text);

                            if (vassocDues == 0 ||Convert.ToString(vassocDues )=="")
                            {
                                vassocDues = 0;
                            }
                           var vtotAssocDues = vassocDues * 12 * vstayYrs * vaccumInflate;

                            //CALCULATE TOTAL PROPERTY TAXES
                          var vpropTax =Convert.ToDouble(txtAnnualPT.Text);
                            if (Convert.ToString( vpropTax) =="" || vpropTax == 0)
                            {
                                vpropTax = 0;
                            }
                           var vtotPropTax = vpropTax * vstayYrs * vaccumInflate;
                           
                            //CALCULATE TOTAL MAINTENANCE COSTS

                        var vmaint = Convert.ToDouble(txtAverageMM.Text);
                            if (Convert.ToString(vmaint)==""||vmaint==0)
                            {
                                vmaint = 0;
                            }
                           var vtotMaintCost = vmaint * 12 * vstayYrs * vaccumInflate;

                            // CALCULATE TOTAL HOMEOWNER INSURANCE COSTS
                          var vhomeIns =Convert.ToDouble(txtHIR.Text);
                            if (Convert.ToString( vhomeIns) =="" || vhomeIns == 0)
                            {
                                vhomeIns = 0;
                            }
                            else
                                if (vhomeIns > .01)
                                {
                                    vhomeIns = vhomeIns / 1000;
                                }
                          var vtotHomeInsCost = vhomeIns * vhomeCost * vstayYrs * vaccumInflate;
                            //CALCULATE NET GAIN ON HOME
                           var vnetGain = vaccumApprec - vhomeCost;

                            //CALCULATE TAX SAVINGS ON INTEREST , POINTS AND PROPERTY TAXES
                          var vtotTaxDeduct =vaccumInt + vtotPropTax + vfeesCost;
                            var vincomeTax = Convert.ToDouble(txtSPFITRate.Text);
                            if (Convert.ToString(vincomeTax) =="" || vincomeTax == 0)
                            {
                                vincomeTax = 0;
                            }
                            else

                                if (vincomeTax >= 1)
                                {
                                    vincomeTax = vincomeTax / 100.0;
                                }
                           var vtotTaxSave = vincomeTax * vtotTaxDeduct;

                            //CALCULATE RELATOR COMMISSION ON SALE OF HOME

                           var vrealtorCom = Convert.ToDouble(txtRelatorCR.Text);
                                if(vrealtorCom==0||Convert.ToString(vrealtorCom)=="")
                                {
                                    vrealtorCom = 0;
                                }
                                else
                                    if (vrealtorCom >= 1)
                                    {
                                        vrealtorCom = vrealtorCom / 100;
                                    }
                                var vsellCost = vaccumApprec * vrealtorCom;
                            //CALCULATE NET ERNINGS ON INVESTMENT
                                var vinvestEarn = vinvestPrin - vdownPmt - vclosingCosts;
                               var vtotRentCosts = vtotRent;
                                var vtotRentBenefits = vinvestEarn;
                               var vnetRentCost = vtotRent - vinvestEarn;
                                var vtotBuyCosts = vaccumInt + vclosingCosts + vtotPropTax + vtotMaintCost + vtotHomeInsCost + vaccumPmi + vtotAssocDues + vsellCost;
                                var vtotBuyBenfits = vnetGain + vtotTaxSave;
                                var vnetBuyCost = vtotBuyCosts - vtotBuyBenfits;
                                App.NetRentCost = "$" + Math.Round(vnetRentCost, 2);
                                App.netBuyCost = "$" + Math.Round(vnetBuyCost, 2);
                                var diff = 0.0;
                                var vsummary = "";
                                if (vnetRentCost > vnetBuyCost)
                                {
                                    diff = vnetRentCost - vnetBuyCost;
                                    vsummary = "You will save $" +Math.Round(diff,2)+" "+"if you buy instead of rent.";
                                }
                                else
                                {
                                    diff = vnetBuyCost - vnetRentCost;
                                    vsummary = "You will save $" + Math.Round(diff, 2) + " " + "if you rent instead of buy.";

                                }
                                App.summary = vsummary;
                        }
           // NavigationService.Navigate(new Uri("/Views/Result/ResultPage.xaml?sectionId=4&", UriKind.Relative));
        }
    }

}